package cn.mysource.org.controller;

import cn.mysource.basic.util.AjaxResult;
import cn.mysource.basic.util.PageList;
import cn.mysource.org.domain.Department;
import cn.mysource.org.domain.Permission;
import cn.mysource.org.query.DepartmentQuery;
import cn.mysource.org.query.PermissionQuery;
import cn.mysource.org.service.IPermissionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/per")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情" )
    public Permission findById(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id) {
        return permissionService.findById(id);

    }


    @GetMapping()
    public List<Permission> findAll() {
        return permissionService.findAll();
    }

    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        if (null != permission.getId()) {
            // 修改
            permissionService.update(permission);
        } else {
            // 添加
            permissionService.add(permission);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        permissionService.delete(id);
        return AjaxResult.me().success();
    }

    //分页查询
    @PostMapping()
    public PageList<Permission> queryByPage(@RequestBody PermissionQuery query) {

        return (PageList<Permission>) permissionService.queryByPage(query);

    }
    //批量删除
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        permissionService.batchDelete(ids);
        return AjaxResult.me().success();
    }

}
